home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Topik / Topik - Disk 02 - Fonts and CLI Commands (19xx)(Topik Public Domain)(PD)[a][WB].zip / Topik - Disk 02 - Fonts and CLI Commands (19xx)(Topik Public Domain)(PD)[a][WB].adf / Source / dirk.c < prev    next >
C/C++ Source or Header  |  1989-04-19  |  10KB  |  376 lines

  1.  
  2. /*
  3.     9/88
  4.  
  5.     DIRK v0.01 -- Tune workbench colors to system performance
  6.  
  7.     Copyright (C) 1988 by Daniel Elbaum
  8.  
  9.     This software is freely redistributable provided:
  10.     the three files which comprise it (dirk, dirk.c, dirk.doc)
  11.     remain intact; that all copyright notices contained in
  12.     any of the aforementioned files remain intact; and that
  13.     no fee beyond reasonable remuneration for collation and
  14.     distribution be charged for use and/or distribution.
  15.  
  16. */
  17.  
  18.  
  19. #include <exec/types.h>
  20. #include <exec/exec.h>
  21. #include <exec/execbase.h>
  22. #include <intuition/intuition.h>
  23.  
  24. #define IREV    (1)
  25. #define GREV    (1)
  26. #define CMAX    (15)    /* max val for color component (hardware)   */
  27.  
  28. /*
  29.     seems odd, but really makes sense, since background
  30.     has 2 color components, and detail has 3.
  31.     These limits are to ensure enough contrast for readability.
  32. */
  33.  
  34. struct g_flags {
  35.     int gran;   /* granularity of mapping from #tasks to pen brightness */
  36.     int bsmax;  /* constant total saturation of background (red+blue)   */
  37.     int dsmax;  /* maximum detail brightness (if high, text glares)     */
  38.     int dsmin;  /* darkest gray for detail pen                          */
  39.     int dstog;  /* set to activate task watch                           */
  40.     int bstog;  /* set to activate memory watch                         */
  41.     int intvl;  /* number of ticks between updates                      */
  42.     int bd_r;   /* base detail pen red component                        */
  43.     int bd_g;   /* base detail pen green component                      */
  44.     int bd_b;   /* base detail pen blue component                       */
  45. }   g_f;    /* initialized in getargs() */
  46.  
  47.  
  48. typedef struct IntuitionBase *  t_ib;
  49. typedef struct GfxBase * t_gb;
  50.  
  51. t_ib IntuitionBase;
  52. t_gb GfxBase;
  53.  
  54. extern struct ExecBase *SysBase;
  55.  
  56. struct IntuiMessage *GetMsg();
  57. void *OpenLibrary();
  58. ULONG AvailMem();
  59.  
  60. main(c, v)
  61. char **v;
  62. {
  63.     register i, active=1;
  64.     register  ULONG tm;
  65.     ULONG fu1, fu2;
  66.     register struct Screen *sp;
  67.     register struct Window *wp;
  68.     register struct IntuiMessage *msg=NULL;
  69.     struct Window *getwin();
  70.     ULONG totmem();
  71.  
  72.     if (getargs(++v, &g_f)<0) exit(10);
  73.  
  74.     if (!(IntuitionBase=(t_ib)OpenLibrary("intuition.library", IREV)))
  75.         exit(99);
  76.     if (!(GfxBase=(t_gb)OpenLibrary("graphics.library", GREV))){
  77.         CloseLibrary(IntuitionBase);
  78.         exit(98);
  79.     }
  80.     if (!(sp=IntuitionBase->FirstScreen)){
  81.         CloseLibrary(IntuitionBase);
  82.         CloseLibrary(GfxBase);
  83.         exit(20);
  84.     }
  85.     for (; sp; sp=sp->NextScreen)
  86.         if (!strcmp(sp->Title, "Workbench Screen"))
  87.             break;
  88.     if (!sp) {
  89.         CloseLibrary(IntuitionBase);
  90.         CloseLibrary(GfxBase);
  91.         exit(22);
  92.     }
  93.     if (!(wp=getwin())) exit(FALSE);
  94.     if (g_f.bstog) tm=totmem();
  95.     for (;;) {
  96.         if (!msg) msg=GetMsg(wp->UserPort);
  97.         if (msg){
  98.             ReplyMsg(msg);
  99.             if (msg->Class==ACTIVEWINDOW)
  100.                 active=1;
  101.             if (msg->Class==INACTIVEWINDOW)
  102.                 active=0;
  103.             if (msg->Class==CLOSEWINDOW){
  104.                 CloseWindow(wp);
  105.                 CloseLibrary(IntuitionBase);
  106.                 CloseLibrary(GfxBase);
  107.                 exit(0);
  108.             }
  109.             msg=NULL;
  110.         }
  111.         if (active)
  112.             WaitTOF();
  113.         else{
  114.             for (i=0; i<g_f.intvl; ++i)
  115.                 if (msg=GetMsg(wp->UserPort)) break;
  116.                 else WaitTOF();
  117.         }
  118.         setcolor(sp, tm);
  119.     }
  120.     /* NOTREACHED   */
  121. }
  122.  
  123. setcolor(sp, tm)
  124. register struct Screen *sp;
  125. ULONG tm;
  126. {
  127.     register f, db, t;
  128.     int tr, tw;
  129.     int r, g, b;
  130.     ULONG mu, fm;
  131.     ULONG amtfree();
  132.  
  133.     if (g_f.dstog){
  134.         tqlen(&tr, &tw);
  135.  
  136.         f=CMAX-(tr+tw)/g_f.gran;
  137.         if (f<g_f.dsmin) f=g_f.dsmin;
  138.         if (f>g_f.dsmax) f=g_f.dsmax;
  139.         if ((t=tr/g_f.gran+f)>CMAX) t=CMAX;
  140.     }
  141.  
  142.     if (g_f.bstog){
  143.         fm=amtfree();
  144.         mu=tm-fm;
  145.         db=(mu*g_f.bsmax)/tm;
  146.     }
  147.  
  148.     /* Detail and Block pens seem to be reversed    */
  149.  
  150.     if ((r=t+g_f.bd_r)>CMAX) r=CMAX;
  151.     if ((g=t+g_f.bd_g)>CMAX) g=CMAX;
  152.     if ((b=t+g_f.bd_b)>CMAX) b=CMAX;
  153.     Forbid();
  154.     if (g_f.dstog) SetRGB4(sp->ViewPort, sp->BlockPen, r, g, b);
  155.     if (g_f.bstog) SetRGB4(sp->ViewPort, sp->DetailPen, db, 0, g_f.bsmax-db);
  156.     Permit();
  157. }
  158.  
  159. /*
  160.     Find the total amount of system memory
  161.     by cruising the master list.
  162. */
  163.  
  164. ULONG
  165. totmem()
  166. {
  167.     register ULONG tu, tl, tm=0;
  168.     register struct Node *n;
  169.     register struct MemHeader *m;
  170.  
  171.     Disable();
  172.     for (n=SysBase->MemList.lh_Head; n->ln_Succ; n=n->ln_Succ){
  173.         m=(struct MemHeader *)n;
  174.         tu=(ULONG)m->mh_Upper;
  175.         tl=(ULONG)m->mh_Lower;
  176.         tm+=tu-tl;
  177.     }
  178.     Enable();
  179.     return(tm);
  180. }
  181.  
  182. /*
  183.     Hunt up all ready tasks and waiting tasks;
  184.     put their respective counts in r and w.
  185. */
  186.  
  187. tqlen(r, w)
  188. int *r, *w;
  189. {
  190.     register tr=0, tw=0;
  191.     register struct Task *t;
  192.  
  193.     Disable();
  194.     t=SysBase->TaskReady.lh_Head;
  195.     for (tr=0; t->tc_Node.ln_Succ; t=t->tc_Node.ln_Succ)
  196.         tr++;
  197.     t=SysBase->TaskWait.lh_Head;
  198.     for (tw=0; t->tc_Node.ln_Succ; t=t->tc_Node.ln_Succ)
  199.         tw++;
  200.     Enable();
  201.     *r=tr;
  202.     *w=tw;
  203.     return(tr+tw);
  204. }
  205.  
  206. /*
  207.     Place amt of free chip mem into cm;
  208.     place amt of free fast mem into fm.
  209. */
  210.  
  211. ULONG
  212. amtfree()
  213. {
  214.     ULONG c, f;
  215.  
  216.     Forbid();
  217.     c=AvailMem(MEMF_CHIP);
  218.     f=AvailMem(MEMF_FAST);
  219.     Permit();
  220.     return(c+f);
  221. }
  222.  
  223. /*
  224.     Just give me a window and don't make me
  225.     put endless initializations into main()
  226.     or global space, okay?
  227. */
  228.  
  229. #define MINWD   (120)
  230. #define MINHT   (10)
  231. #define MAXWD   MINWD
  232. #define MAXHT   MINHT
  233.  
  234. struct Window *
  235. getwin()
  236. {
  237.     struct NewWindow nw;
  238.     struct Window *Window;
  239.  
  240.     nw.LeftEdge = 640/2-MAXWD/2;
  241.     nw.TopEdge = 0;
  242.     nw.Width = MAXWD;
  243.     nw.Height = MAXHT;
  244.     nw.DetailPen = 0;
  245.     nw.BlockPen = 1;
  246.     nw.Flags = WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|BORDERLESS;
  247.     nw.IDCMPFlags = CLOSEWINDOW|ACTIVEWINDOW|INACTIVEWINDOW;
  248.     nw.FirstGadget = NULL;
  249.     nw.CheckMark = NULL;
  250.     nw.Title="Dirk";
  251.     nw.Type=WBENCHSCREEN;
  252.     nw.Screen = NULL;
  253.     nw.BitMap = NULL;
  254.     nw.MinWidth = MINWD;
  255.     nw.MinHeight = MINHT;
  256.     nw.MaxWidth = MAXWD;
  257.     nw.MaxHeight = MAXHT;
  258.     if (!(Window = OpenWindow(&nw))) {
  259.         printf("can't open window");
  260.         return((struct Window *)NULL);
  261.     }
  262.     else return(Window);
  263. }
  264.  
  265. getargs(v, f)
  266. char **v;
  267. struct g_flags *f;
  268. {
  269.     register out=0;
  270.  
  271.     f->gran  = 4;      /*  good when 4-5 processes are in background   */
  272.     f->bsmax = 14;     /*  good background--nor too bright nor dark    */
  273.     f->dsmax = 12;     /*  takes some glare out of text                */
  274.     f->dsmin = 8;      /*  readable even at near-red                   */
  275.     f->dstog = 1;      /*  task watcher on by default                  */
  276.     f->bstog = 1;      /*  memory watcher on by default                */
  277.     f->intvl = 180;    /*  default delay is 3 seconds                  */
  278.     f->bd_r  = 0;      /*  increment for detail pen red                */
  279.     f->bd_g  = 0;      /*  increment for detail pen green              */
  280.     f->bd_b  = 0;      /*  increment for detail pen blue               */
  281.  
  282.     while (*v){
  283.         if (**v=='-') {
  284.             if (!(*v)[1]) return(usage());
  285.             while (*++*v){
  286.                 switch(**v){
  287.                     case 'g': f->gran=atoi(++*v); ++out; break;
  288.                     case 'b': f->bsmax=atoi(++*v); ++out; break;
  289.                     case 'h': f->dsmax=atoi(++*v); ++out; break;
  290.                     case 'l': f->dsmin=atoi(++*v); ++out; break;
  291.                     case 'i': f->intvl=atoi(++*v); ++out; break;
  292.                     case 't': f->bstog=0; break;
  293.                     case 'm': f->dstog=0; break;
  294.                     default: return(usage());
  295.                 }                               /* switch (**v) */
  296.                 if (out) {++v; break;}
  297.             }                               /* while (*(++*v))  */
  298.             if (!out) ++v;
  299.             else out=0;
  300.         }                               /* if (**v=='-')    */
  301.         else if (**v=='+') {
  302.             if (!(*v)[1]) return(usage());
  303.             while (*++*v){
  304.                 switch(**v){
  305.                     case 'r': f->bd_r=atoi(++*v); ++out; break;
  306.                     case 'g': f->bd_g=atoi(++*v); ++out; break;
  307.                     case 'b': f->bd_b=atoi(++*v); ++out; break;
  308.                     default: return(usage());
  309.                 }                               /* switch (**v) */
  310.                 if (out) {++v; break;}
  311.             }                               /* while (*++*v)  */
  312.             if (!out) ++v;
  313.             else out=0;
  314.         }                               /* else if (**v=='+')   */
  315.         else {
  316.             return(usage());
  317.         }
  318.     }                               /* while (*v)   */
  319.     limit(f);
  320.     if (f->gran==0) f->gran=1;  /* special case for x/gran  */
  321.     return(0);
  322. }
  323.  
  324. #define LFIX(x, l)   if (x<l) x=l
  325. #define HFIX(x, h)   if (x>h) x=h
  326.  
  327. /*
  328.     Make sure all members of *f are in bounds.
  329. */
  330.  
  331. limit(f)
  332. struct g_flags *f;
  333. {
  334.     int maxint=(1<<sizeof(maxint))-1;
  335.  
  336.  
  337.     LFIX(f->gran, 0);
  338.     HFIX(f->gran, maxint);
  339.     LFIX(f->bsmax, 0);
  340.     HFIX(f->bsmax, CMAX);
  341.     LFIX(f->dsmax, 0);
  342.     HFIX(f->dsmax, CMAX);
  343.     LFIX(f->dsmin, 0);
  344.     HFIX(f->dsmin, CMAX);
  345.     LFIX(f->intvl, 0);
  346.     HFIX(f->intvl, maxint);
  347.  
  348.     LFIX(f->bd_r, 0);
  349.     HFIX(f->bd_r, CMAX);
  350.     LFIX(f->bd_g, 0);
  351.     HFIX(f->bd_g, CMAX);
  352.     LFIX(f->bd_b, 0);
  353.     HFIX(f->bd_b, CMAX);
  354. }
  355.  
  356. usage()
  357. {
  358.     printf("dirk v0.01 copyright (c) 1988 Daniel Elbaum/Amaranth Software\n");
  359.     printf("\nUsage: dirk [-t|m] [-gN] [-bN] [-hN] [-lN] [-iN]]\n");
  360.     printf("t\ttrack tasks only\n");
  361.     printf("m\ttrack memory only\n");
  362.     printf("g   (4)\tgranularity of task mapping (small for few tasks)\n");
  363.     printf("b  (14)\tbackground saturation\n");
  364.     printf("h  (12)\tmaximum detail saturation\n");
  365.     printf("l   (8)\tminimum detail saturation\n");
  366.     printf("i (180)\tinterval in ticks (60 or 50 per second)\n");
  367.     printf("\n\t[+rN] [+gN] [+bN]\n");
  368.     printf("r   (0)\tamount by which to redden the pen\n");
  369.     printf("g   (0)\tamount by which to greeen the pen\n");
  370.     printf("b   (0)\tamount by which to bluen the pen\n");
  371.     printf("\n");
  372.     return(-1);
  373. }
  374.  
  375.  
  376.